﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class ClientForm : FormBase,ISubject, IObserver
    {
        Validations valid = new Validations();
        private EDM_AuctionContainer context;
        int segundosRestantes=40;
        int x = 0;
        decimal virtualBond = 0;
        Auction auctionActive = null;
        Package packageActive = null;
        Bidder bidder = null;
        private ISubject subject = null;

       // private List<IObserver> observers { get; set; }
        public void AddObservers(IObserver observer)
        {
       
            //base.AddObservers(observer);
        }
        public  void RemoveObserver(IObserver observer)
        {
            //base.RemoveObserver(observer);
        }
        public void Notify()
        {
            //foreach (IObserver observer in base.getObservers())
            //{
            //    observer.Update(t_bidWinner.Text, t_time.Text, segundosRestantes);
            //}
        }
        public void Update(string bid,string time, int seconds)
        {
            //t_bidWinner.Text = bid;
            //t_time.Text = time;
            //segundosRestantes = seconds;
        }

      
        public ClientForm(EDM_AuctionContainer context,Bidder bidder)
        {
            this.subject = this;
            this.context = context;
            this.bidder = bidder;
            subject.AddObservers(this);
            try
            {
                auctionActive = context.AuctionSet.FirstOrDefault(a => a.active == true);
            }
            catch (System.Exception)
            {
                auctionActive = null;
            }
            if (auctionActive==null)
            {
                MessageBox.Show("Sorry, we haven't auction actived for you in this moment", "Operation cancelled", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
              
                try
                {
                    packageActive = context.PackageSet.FirstOrDefault(p => p.putUpForSale == false);
                }
                catch (System.Exception)
                {
                    packageActive = null;
                }
                if (packageActive == null)
                {
                    MessageBox.Show("Sorry, we haven't package actived for you in this moment", "Operation cancelled", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                  //  bidder = context.BidderSet.FirstOrDefault();
                   
                    virtualBond = bidder.Bond*10;
                    InitializeComponent();
                    preparePanel();
                   
                }
            }
        }

        private void preparePanel()
        {
            x = 0; 
            t_amount.Text = packageActive.outputPrice.ToString();
            t_bidWinner.Text = packageActive.outputPrice.ToString();
            t_description.Text = packageActive.description;
            t_NameOfAuction.Text = packageActive.nameOfPackage;
            t_namePackage.Text = packageActive.nameOfPackage;
            t_bond.Text = bidder.Bond.ToString();

            foreach (WorkOfArt aWork in packageActive.WorkOfArt)
            {
                dG_Packages.Rows.Add();
                dG_Packages.Rows[x].Cells[authorName.Index].Value = aWork.AuthorName;
                dG_Packages.Rows[x].Cells[nameWork.Index].Value = aWork.nameOfWork;
                dG_Packages.Rows[x].Cells[authorSurname.Index].Value = aWork.AuthorSurname;
                dG_Packages.Rows[x].Cells[Year.Index].Value = aWork.year;
                x++;
            }
         
            if (timer1.Enabled == false)
            {
                timer1.Start();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int minutes = segundosRestantes/60;
            int segundos =segundosRestantes-(minutes * 60);
            if (segundos < 10)
            {
                if (minutes == 0)
                {
                    t_time.ForeColor = Color.Red;
                }
                t_time.Text = minutes + ":0" + segundos;
            }
            else
            {
                t_time.Text = minutes + ":" + segundos;
            }
            if (segundosRestantes != 0)
            {
                segundosRestantes--;
                Notify();
               
            }
            else 
            {
                timer1.Stop();
                packageActive.putUpForSale = true;
                context.PackageSet.ApplyCurrentValues(packageActive);
                context.SaveChanges();
                Auction checkAuction;
                Bid bid;
                try
                {
                    checkAuction = context.AuctionSet.FirstOrDefault(a => a.active == true);
                }
                catch (System.Exception)
                {
                    checkAuction = null;
                    auctionActive.active = false;
                }
                 try
                {
                    bid = context.BidSet.ElementAt(context.BidSet.Count());
                    if (packageActive.minimunSellingPrice < bid.Amount)
                    {
                        packageActive.sold = true;
                        context.PackageSet.ApplyCurrentValues(packageActive);
                        context.SaveChanges();
                        if (bid.Bidder.idBidder == bidder.idBidder && bid.Auction.idAuction == auctionActive.idAuction)
                        {
                            bidder.Bond = valid.GetDecimal(t_bond);
                            context.BidderSet.ApplyCurrentValues(bidder);
                            context.SaveChanges();
                        }
                    }
                }
                 catch (System.Exception)
                {
                    checkAuction = null;
                    auctionActive.active = false;
                }
                
                
            }
        }

        private void b_add100_Click(object sender, EventArgs e)
        {
            t_amount.Text = (valid.GetDecimal(t_amount) + 100).ToString();
        }

        private void b_add200_Click(object sender, EventArgs e)
        {
            t_amount.Text = (valid.GetDecimal(t_amount) + 200).ToString();
        }

        private void b_add500_Click(object sender, EventArgs e)
        {
            t_amount.Text = (valid.GetDecimal(t_amount) + 500).ToString();
        }

        private void b_add1000_Click(object sender, EventArgs e)
        {
            t_amount.Text = (valid.GetDecimal(t_amount) + 1000).ToString();
        }

        private void b_add2000_Click(object sender, EventArgs e)
        {
            t_amount.Text = (valid.GetDecimal(t_amount) + 2000).ToString();
        }

        private void b_add5000_Click(object sender, EventArgs e)
        {
            t_amount.Text = (valid.GetDecimal(t_amount) + 5000).ToString();
        }

        private void b_bid_Click(object sender, EventArgs e)
        {
            decimal amountBided=0; 
            if (valid.GetDecimal(t_amount) > valid.GetDecimal(t_bidWinner))
            {
                if (bidder.Bond *10 == virtualBond)
                {
                    amountBided = valid.GetDecimal(t_amount);
                }
                else
                {
                    amountBided=valid.GetDecimal(t_amount) - valid.GetDecimal(t_bidWinner);
                }
                if (amountBided-1 < virtualBond)
                {
                    virtualBond = virtualBond - amountBided;
                    t_bidWinner.Text = t_amount.Text;
                    Bid bidMade = new Bid();
                    bidMade.Amount = amountBided;
                    bidMade.Date = DateTime.Today;
                    bidMade.Bidder = bidder;
                    bidMade.Auction = auctionActive;
                    t_bond.Text = virtualBond.ToString();
                    l_winner.Visible = true;
                    
                    //comprobar si esta entre los 10 ultimos segundos
                    context.BidSet.AddObject(bidMade);
                    if (segundosRestantes < 10)
                    {
                        segundosRestantes += 30;
                    }
                    Notify();
                    
                }
                else 
                {
                    MessageBox.Show("You haven't enough funds to bid this amount", "Bid not valid", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("You must bid an amount greater than the present winning bid", "Bid not valid", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

      
  
    }
}
